package com.ajguan.library.view;


import com.ajguan.library.ILoadMoreView;
import com.ajguan.library.ResourceTable;
import com.github.ybq.core.SpinKitView;
import com.github.ybq.core.SpriteFactory;
import com.github.ybq.core.Style;
import com.github.ybq.core.sprite.Sprite;
import ohos.agp.components.*;
import ohos.app.Context;


/**
 * Created by guanaj on 16/9/22.
 */

public class SimpleLoadMoreView extends StackLayout implements ILoadMoreView {

    private Text tvHitText;
    private SpinKitView spinKitView;
    private Component view;

    public SimpleLoadMoreView(Context context) {
        this(context, null);
    }

    public SimpleLoadMoreView(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    public SimpleLoadMoreView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        view = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_default_load_more, this, true);
        tvHitText = (Text) view.findComponentById(ResourceTable.Id_tv_hit_content);
        spinKitView = (SpinKitView) view.findComponentById(ResourceTable.Id_spin_kit);
        Sprite sprite = SpriteFactory.create(Style.CIRCLE);
        sprite.setPaintColor(context.getColor(ResourceTable.Color_accent));
        sprite.setBounds(spinKitView.getPaddingLeft(),
                spinKitView.getPaddingTop(),
                spinKitView.getWidth()-spinKitView.getPaddingRight(),
                spinKitView.getHeight()-spinKitView.getPaddingBottom());
        sprite.setComponent(spinKitView);
        spinKitView.setIndeterminate(true);
        spinKitView.addDrawTask((component, canvas) -> sprite.drawToCanvas(canvas));
    }

    @Override
    public void reset() {
        spinKitView.setVisibility(INVISIBLE);
        tvHitText.setVisibility(INVISIBLE);
        tvHitText.setText("正在加载...");
    }

    @Override
    public void loading() {
        spinKitView.setVisibility(VISIBLE);
        tvHitText.setVisibility(VISIBLE);
        tvHitText.setText("正在加载...");
    }

    @Override
    public void loadComplete() {
        spinKitView.setVisibility(INVISIBLE);
        tvHitText.setVisibility(VISIBLE);
        tvHitText.setText("加载完成");

    }

    @Override
    public void loadFail() {
        spinKitView.setVisibility(INVISIBLE);
        tvHitText.setVisibility(VISIBLE);
        tvHitText.setText("加载失败,点击重新加载");

    }

    @Override
    public void loadNothing() {
        spinKitView.setVisibility(INVISIBLE);
        tvHitText.setVisibility(VISIBLE);
        tvHitText.setText("没有更多可以加载");
    }

    @Override
    public Component getCanClickFailView() {
        return view;
    }


}
